package net.zhangyun.futures;

import net.zhangyun.utils.*;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.stream.LongStream;

public class FutureMain {

    private static ExecutorService executorService = new ForkJoinPool();

    public static void main(String[] args) throws Exception {
        blockFuture();
        completableFuture();
    }

    private static void blockFuture() throws Exception {

        java.util.concurrent.Future<Long> intComputation = executorService.submit(() -> {
            // Synchronize operation
            return LongStream.range(1, 1000000000L).sum();
        });

        // intComputation.get() is block operation
        MyConsole.printSingleLambda(intComputation::get);
    }

    private static void completableFuture() throws Exception {
        CompletableFuture<Long> completableFuture = new CompletableFuture<>();

        executorService.submit(() -> {
            // Synchronize operation
            completableFuture.complete(LongStream.range(1, 1000000000L).sum());
        });

        completableFuture.thenAccept(MyConsole::printSingle);

        Thread.sleep(3000);
    }
}
